<!DOCTYPE html>
<html>

	<head lang="en">
		<title>骡窝日报</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/news.css" />
		<link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
		<script src="/js/plugins/jrender/jrender.js"></script>
		<link rel="stylesheet" href="/js/plugins/jquery-confirm/css/jquery-confirm.css">
		<script src="/js/plugins/jquery-confirm/js/jquery-confirm.js"></script>
		<script src="/js/plugins/dialog/dialog.min.js"></script>
		<script src="/js/common.js"></script>

		</head>

	<body>
		

		<div class="search-head">
			<div class="row nav-search">
				<div class="col-2">
					<a href="index.html">
						<span><i class="fa fa-chevron-left fa-2x"></i></span>
					</a>
				</div>
				<div class="col-10">
					<div class="input-group-sm search">
						<input class="form-control searchBtn" placeholder="搜索资讯">
					</div>
				</div>
			</div>
		</div>

		<div class="container news">
		
			<div class="news collectEle">
				<h6 class="news-title">骡窝日报 </h6>
				<div class="row" render-loop="list">
					<div class="col-6">
						<a data-id="2" render-key="list.id" render-fun="handle">
							<img class="rounded" width="100%" height="110px"  render-src="list.coverUrl">
						</a>
						<div class="caption">
							<p>
								<span > </span>
								<i class="fa" style="color:red" id="collectBtn" render-key="list.id" render-fun="collectEvent"></i>
							</p>
							<a href="newsContent.html" render-key="list.id" render-fun="handle">
							<p render-html="list.title"></p>
							</a>
						</div>
					</div>
				</div>
			</div>
		</div>

	<script>
        // 获取用户信息
        var user = JSON.parse(sessionStorage.getItem("user"));
		//定义当前页
        var currentPage = 1;
		//定义总页数
        var pages;
		//定义总日报数
        var news;
		$(function () {

		    //发起请求查询所有日报进行渲染
			$.get("/news",{currentPage:currentPage,pageSize:6},function (data) {
			    pages = data.pages;
			    news = data.list;
				$(".news").renderValues(data,{
				    handle:function (ele, value) {
						ele.href = "/newsContent.html?id=" + value;
                    },
                    collectEvent:function (ele, value) {
                        $(ele).attr("data-tid",value);
                    }
				})


				//渲染完毕后查询用户收藏的日报
                if (user){
                    $.get("/collect",{userId:user.id,type:1,pageSize:0},function (data) {
                        //先设置所有为
                        $(".collectEle i[data-tid]").addClass("fa-heart-o");
                        console.log(data.list);
                        $.each(data.list,function (index,value) {
                            console.log(index,value);
                            $(".collectEle i[data-tid="+value.id+"]").removeClass("fa-heart-o");
                            $(".collectEle i[data-tid="+value.id+"]").addClass("fa-heart");
                        })
                    })
                }else {
                    $(".collectEle i[data-tid]").addClass("fa-heart-o");
				}



            });


			//设置滑动事件
			$(window).scroll(function () {
				//判断滑动事件是否到底
			    if (isEnd()){
			        //判断是否是最后一页
					if (currentPage < pages) {
					    currentPage ++;
                        $.get("/news",{currentPage:currentPage,pageSize:6},function (data) {
                            $.merge(news,data.list);
                            $(".news").renderValues({list:news},{

                                handle:function (ele, value) {
                                    ele.href = "/newsContent.html?id=" + value;
                                },
                                collectEvent:function (ele, value) {
                                    $(ele).attr("data-tid",value);
                                }
                            })
							//滚动事件后判断是否有值
                            if (user){
                                //发起请求查询该用户收藏的日报
                                $.get("/collect",{userId:user.id,type:1,pageSize:0},function (data) {
                                    //先设置所有为空心
                                    $(".collectEle i[data-tid]").addClass("fa-heart-o");
                                    //迭代数据
                                    $.each(data.list,function (index,value) {
                                        //清空选中的标签的空心
                                        $(".collectEle i[data-tid="+value.id+"]").removeClass("fa-heart-o");
                                        //设置为实心
                                        $(".collectEle i[data-tid="+value.id+"]").addClass("fa-heart");
                                    })
                                })
                            }else {
                                $(".collectEle i[data-tid]").addClass("fa-heart-o");
                            }

                        })

					}else {
                        // 否则, 提示已到低
                        $(document).dialog({
                            type : 'notice',
                            infoText: '到底了',
                            autoClose: 2500,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
					}


				}

            })

        })



			var ele;
            // 动态绑定收藏爱心按钮事件
            $(".collectEle").on("click", "i[data-tid]", function () {
				ele = this;
                if (user){
                var id = $(this).attr("data-tid");
                console.log(id);
                	$.ajax({
						url: "/collect",
						type: "POST",
						data: {userId: user.id, collectId:id, type: 1},
						success: function (data) {
							if (data){
							    $(ele).removeClass("fa-heart");
								$(ele).addClass("fa-heart-o");
                            }else {
							    $(ele).removeClass("fa-heart-o");
							    $(ele).addClass("fa-heart");
							}
                    	}
                	});
                }else {
                    // 跳转到登录页面进行登录操作
                    $(document).dialog({
                        type: "confirm",
                        titleShow: false,
                        content: '您尚未登录',
                        buttons: [
                            {
                                name: '跳转到登录页面',
                                callback: function () {
                                    location.href = "/login.html";
                                }
                            }
                        ]
                    });
                }
            });


        // 定义一个判断屏幕溢出高度 + 屏幕高度 > 网页高度的函数
        function isEnd() {
            return $(window).height() + parseInt( $(window).scrollTop() + 1 ) >= $(document).height();
        }
	</script>
</body>
</html>